﻿@model FileViewModel

@{
    ViewBag.Title = "title";
}

<script src="@Url.Content("~/Resources/dynamsoft.webtwain.initiate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Resources/dynamsoft.webtwain.config.js")" type="text/javascript"></script>


@*<script src="/Resources/dynamsoft.webtwain.initiate.js" type="text/javascript"></script>
<script src="/Resources/dynamsoft.webtwain.config.js" type="text/javascript"></script>*@

<input type="button" value="Scan and Save" onclick="AcquireImage();" />
<input type="button" value="Load" onclick="LoadImage();" />
<input type="button" value="Save" onclick="SaveWithFileDialog();" />
<br />
<label><input type="radio" value="jpg" name="ImageType" id="imgTypejpeg" />JPEG</label>
<label><input type="radio" value="tif" name="ImageType" id="imgTypetiff" />TIFF</label>
<label><input type="radio" value="pdf" name="ImageType" id="imgTypepdf" checked="checked" />PDF</label>


<div id="dwtcontrolContainer"></div>

<script type="text/javascript">
    Dynamsoft.WebTwainEnv.RegisterEvent('OnWebTwainReady', Dynamsoft_OnReady); // Register OnWebTwainReady event. This event fires as soon as Dynamic Web TWAIN is initialized and ready to be used

    var DWObject;

    function Dynamsoft_OnReady() {
        DWObject = Dynamsoft.WebTwainEnv.GetWebTwain('dwtcontrolContainer'); // Get the Dynamic Web TWAIN object that is embeded in the div with id 'dwtcontrolContainer'
        if (DWObject) {
            DWObject.RegisterEvent('OnPostAllTransfers', SaveWithFileDialog);
        }
    }

    function AcquireImage() {
        if (DWObject) {
            DWObject.SelectSource();
            DWObject.OpenSource();
            DWObject.IfDisableSourceAfterAcquire = true;	// Scanner source will be disabled/closed automatically after the scan.
            DWObject.AcquireImage();
        }
    }

    //Callback functions for async APIs
    function OnSuccess() {
        console.log('successful');
    }

    function OnFailure(errorCode, errorString) {
        alert(errorString);
    }

    function LoadImage() {
        if (DWObject) {
            DWObject.IfShowFileDialog = true; // Open the system's file dialog to load image
            DWObject.LoadImageEx("", EnumDWT_ImageType.IT_ALL, OnSuccess, OnFailure); // Load images in all supported formats (.bmp, .jpg, .tif, .png, .pdf). OnSuccess or OnFailure will be called after the operation
        }
    }

    function SaveWithFileDialog() {
        if (DWObject) {
            if (DWObject.HowManyImagesInBuffer > 0) {
                DWObject.IfShowFileDialog = true;
                if (document.getElementById("imgTypejpeg").checked == true) {
                    //If the current image is B&W
                    //1 is B&W, 8 is Gray, 24 is RGB
                    if (DWObject.GetImageBitDepth(DWObject.CurrentImageIndexInBuffer) == 1)
                        //If so, convert the image to Gray
                        DWObject.ConvertToGrayScale(DWObject.CurrentImageIndexInBuffer);
                    //Save image in JPEG
                    DWObject.SaveAsJPEG("DynamicWebTWAIN.jpg", DWObject.CurrentImageIndexInBuffer);
                }
                else if (document.getElementById("imgTypetiff").checked == true)
                    DWObject.SaveAllAsMultiPageTIFF("DynamicWebTWAIN.tiff", OnSuccess, OnFailure);
                else if (document.getElementById("imgTypepdf").checked == true)
                    DWObject.SaveAllAsPDF("DynamicWebTWAIN.pdf", OnSuccess, OnFailure);
            }
        }
    }
</script>
